package com.njwb.Swing.Users;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;

public class UserView extends JPanel {
    private JTable userTable;
    private DefaultTableModel tableModel;

    private JTextField searchField;
    private JButton searchButton;
    private JButton addButton;
    private JButton editButton;
    private JButton deleteButton;

    public UserView() {
        setLayout(new BorderLayout(5, 5));
        setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        // 顶部面板
        JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 5));
        topPanel.setBackground(new Color(240, 248, 255));
        topPanel.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));

        searchField = new JTextField(15);
        searchButton = new JButton("搜索");
        addButton = new JButton("新增用户");
        editButton = new JButton("修改用户");
        deleteButton = new JButton("删除用户");

        Dimension btnSize = new Dimension(100, 28);
        searchButton.setPreferredSize(new Dimension(70, 28));
        addButton.setPreferredSize(btnSize);
        editButton.setPreferredSize(btnSize);
        deleteButton.setPreferredSize(btnSize);

        topPanel.add(new JLabel("用户名："));
        topPanel.add(searchField);
        topPanel.add(searchButton);
        topPanel.add(addButton);
        topPanel.add(editButton);
        topPanel.add(deleteButton);

        add(topPanel, BorderLayout.NORTH);

        // 表格
        String[] columns = {"ID", "用户名", "密码", "性别", "年龄", "电话", "地址", "角色"};
        tableModel = new DefaultTableModel(columns, 0) {
            @Override
            public boolean isCellEditable(int row, int column) { return false; }
        };
        userTable = new JTable(tableModel);
        userTable.setRowHeight(28);
        userTable.setFont(new Font("宋体", Font.PLAIN, 14));
        userTable.setSelectionBackground(new Color(173, 216, 230));
        userTable.getTableHeader().setFont(new Font("宋体", Font.BOLD, 14));
        userTable.getTableHeader().setBackground(new Color(200, 220, 240));

        // 隐藏密码列
        userTable.getColumnModel().getColumn(2).setMinWidth(0);
        userTable.getColumnModel().getColumn(2).setMaxWidth(0);
        userTable.getColumnModel().getColumn(2).setWidth(0);
        userTable.getColumnModel().getColumn(2).setPreferredWidth(0);

        JScrollPane scrollPane = new JScrollPane(userTable);
        scrollPane.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
        add(scrollPane, BorderLayout.CENTER);
    }

    // getter 方法，供 Controller 使用
    public JTable getUserTable() { return userTable; }
    public DefaultTableModel getTableModel() { return tableModel; }
    public JTextField getSearchField() { return searchField; }
    public JButton getSearchButton() { return searchButton; }
    public JButton getAddButton() { return addButton; }
    public JButton getEditButton() { return editButton; }
    public JButton getDeleteButton() { return deleteButton; }
}
